package handler

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func (h *Handler) Login(c *gin.Context) {
	//定义一下body里面json解析出来的参数
	var req struct {
		UserID   string `json:"account" binding:"required"`
		Password string `json:"password" binding:"required"`
	}
	//解析json
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	refreshToken, accessToken, success, err := h.userService.Login(req.UserID, req.Password, h.jwtMiddleware)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	if !success {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"refreshToken": refreshToken, "accessToken": accessToken})
}

func (h *Handler) Signup(c *gin.Context) {
	var req struct {
		Account  string `json:"account" binding:"required"`
		Email    string `json:"email"`
		Nickname string `json:"nickname" binding:"required"`
		Password string `json:"password" binding:"required"`
		Phone    string `json:"phone"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	err := h.userService.Signup(req.Account, req.Email, req.Nickname, req.Password, req.Phone)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
}

func (h *Handler) RefreshToken(c *gin.Context) {
	var req struct {
		RefreshToken string `json:"refreshToken" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	refreshToken, accessToken, err := h.userService.RefreshToken(req.RefreshToken, h.jwtMiddleware)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"refreshToken": refreshToken, "accessToken": accessToken})
}

func (h *Handler) UpdateUserInfo(c *gin.Context) {
	var req struct {
		Account  string  `json:"account" binding:"required"`
		Email    *string `json:"email"`
		Nickname *string `json:"nickname"`
		Password *string `json:"password"`
		Phone    *string `json:"phone"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 创建更新字段的map
	updates := make(map[string]interface{})

	// 只添加需要更新的字段
	if req.Email != nil {
		updates["email"] = *req.Email
	}
	if req.Nickname != nil {
		updates["nickname"] = *req.Nickname
	}
	if req.Password != nil {
		updates["password"] = *req.Password
	}
	if req.Phone != nil {
		updates["phone"] = *req.Phone
	}

	err := h.userService.UpdateUserInfo(req.Account, updates)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "用户信息更新成功"})
}

func (h *Handler) GetReviewStatus(c *gin.Context) {
	// Implementation for GetReviewStatus
}

func (h *Handler) Logout(c *gin.Context) {
	userID := c.GetString("account")
	err := h.userService.Logout(userID, h.jwtMiddleware)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "log out successfully"})
}

func (h *Handler) GetUserMessages(c *gin.Context) {
	// Implementation for GetUserMessages
}
